package com.basic.modules.ykf.controller;

import java.util.Arrays;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.basic.common.exception.RRException;
import com.basic.common.utils.ParamsModel;
import com.basic.common.validator.ValidatorUtils;

import com.basic.modules.sys.entity.SysUserEntity;
import com.basic.modules.ykf.service.YkfDictDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.basic.modules.ykf.entity.YkfCompanyDeptEntity;
import com.basic.modules.ykf.service.YkfCompanyDeptService;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.R;



/**
 * 部门管理
 *
 * @author maoheng
 * @email 15738859597@163.com
 * @date 2024-09-08 16:23:03
 */
@RestController
@RequestMapping("ykf/companyDept")
public class YkfCompanyDeptController {

    @Autowired
    private YkfCompanyDeptService ykfCompanyDeptService;

    @Autowired
    private YkfDictDetailService ykfDictDetailService;

    /**
     * 列表-分页
     */
    @PostMapping("/page")
    public R list(@RequestBody ParamsModel<YkfCompanyDeptEntity> params){
        PageUtils page = ykfCompanyDeptService.queryPage(params);
        return R.ok(page);
    }


    /**
     * 信息
     */
    @PostMapping("/info")
    public R info(@RequestBody YkfCompanyDeptEntity params){
        if (ObjectUtil.isEmpty(params) || ObjectUtil.isEmpty(params.getDeptId())) {
            throw new RRException("参数缺失");
        }
        return R.ok(ykfCompanyDeptService.getById(params.getDeptId()));
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody YkfCompanyDeptEntity ykfCompanyDept){
        ValidatorUtils.validateEntity(ykfCompanyDept);
		ykfCompanyDeptService.save(ykfCompanyDept);
        ykfDictDetailService.updateDictMap("department", StrUtil.toString(ykfCompanyDept.getDeptId()), ykfCompanyDept.getDeptName());
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody YkfCompanyDeptEntity ykfCompanyDept){
        ValidatorUtils.validateEntity(ykfCompanyDept);
		ykfCompanyDeptService.updateById(ykfCompanyDept);
        ykfDictDetailService.updateDictMap("department", StrUtil.toString(ykfCompanyDept.getDeptId()), ykfCompanyDept.getDeptName());
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] deptIds){
		ykfCompanyDeptService.removeByIds(Arrays.asList(deptIds));
        for (Long deptId : deptIds) {
            ykfDictDetailService.updateDictMap("department", StrUtil.toString(deptId), "");
        }
        return R.ok();
    }

    /**
     * 当前登录人市场部人员列表-分页
     */
    @PostMapping("/currentCompanyDeptPage")
    public R currentCompanyDeptPage(@RequestBody ParamsModel<SysUserEntity> params){
        PageUtils page = ykfCompanyDeptService.currentCompanyDeptPage(params);
        return R.ok(page);
    }

}
